<template>
  <el-form :model="form" label-position="top">
    <el-form-item label="行业名称" required>
      <el-autocomplete
          v-model="form.industryName"
          :fetch-suggestions="queryIndustrySearch"
          placeholder="请输入行业名称"
          clearable
          style="width: 100%"
          :trigger-on-focus="true"
          value-key="name"
          @select="handleSelect"
      />
    </el-form-item>
    <el-form-item label="最小风险数量">
      <el-input-number 
        v-model="form.minRiskCount" 
        :min="1" 
        :max="100" 
        style="width: 100%" 
      />
    </el-form-item>
    <el-form-item>
      <el-button 
        type="primary" 
        @click="handleQuery" 
        :loading="loading"
        :disabled="!form.industryName"
      >
        <el-icon><Search /></el-icon> 查询分析
      </el-button>
    </el-form-item>
    
    <el-alert
      v-if="form.industryName"
      type="info"
      show-icon
      :closable="false"
    >
      将分析 {{ form.industryName }} 行业中风险数量 ≥ {{ form.minRiskCount }} 的企业
    </el-alert>
  </el-form>
</template>

<script>
import { ref, reactive } from 'vue';
import { Search } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { searchIndustries } from '@/api/graphAnalysis';

export default {
  name: 'CompanyRiskForm',
  components: {
    Search
  },
  props: {
    loading: {
      type: Boolean,
      default: false
    }
  },
  emits: ['query', 'select'],
  setup(props, { emit }) {
    const form = reactive({
      industryName: '',
      industryId: null,
      minRiskCount: 10
    });

    const queryIndustrySearch = async (queryString, callback) => {
      try {
        const response = await searchIndustries(queryString || '');
        callback(response.data || []);
      } catch (error) {
        console.error('搜索行业失败:', error);
        ElMessage.error('搜索行业失败，请稍后重试');
        callback([]);
      }
    };

    const handleSelect = (item) => {
      form.industryId = item.id;
      form.industryName = item.name;
    };

    const handleQuery = () => {
      if (!form.industryName) return;
      
      emit('query', {
        industryName: form.industryName.trim(),
        industryId: form.industryId,
        minRiskCount: form.minRiskCount
      });
    };

    return {
      form,
      queryIndustrySearch,
      handleSelect,
      handleQuery
    };
  }
};
</script>

<style scoped>
.el-form {
  width: 100%;
}

.el-button {
  width: 100%;
  margin-top: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.el-input-number {
  width: 100%;
}

:deep(.el-alert) {
  margin-top: 16px;
}
</style> 